/*
 * RestaurantesController.java
 *
 * Created on 3 de Dezembro de 2007, 08:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.no_ip.gerson.persistencia;

import java.util.ArrayList;
import java.util.Collection;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author gerson.albuquerque
 */
public class RestaurantesController {
    
    /** Creates a new instance of RestaurantesController */
    public RestaurantesController() {
        emf = Persistence.createEntityManagerFactory("Qualeaboa");
    }

    private Restaurantes restaurantes;

    private DataModel model;

    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    private int batchSize = 10;

    private int firstItem = 0;

    public Restaurantes getRestaurantes() {
        return restaurantes;
    }

    public void setRestaurantes(Restaurantes restaurantes) {
        this.restaurantes = restaurantes;
    }

    public DataModel getDetailRestaurantess() {
        return model;
    }

    public void setDetailRestaurantess(Collection<Restaurantes> m) {
        model = new ListDataModel(new ArrayList(m));
    }

    public String createSetup() {
        this.restaurantes = new Restaurantes();
        return "restaurantes_create";
    }

    public String create() {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(restaurantes);
            em.getTransaction().commit();
            addSuccessMessage("Restaurantes was successfully created.");
        } catch (Exception ex) {
            try {
                addErrorMessage(ex.getLocalizedMessage());
                em.getTransaction().rollback();
            } catch (Exception e) {
                addErrorMessage(e.getLocalizedMessage());
            }
        } finally {
            em.close();
        }
        return "restaurantes_list";
    }

    public String detailSetup() {
        setRestaurantesFromRequestParam();
        return "restaurantes_detail";
    }
    
    public String detailSetupAjax() {
    setRestaurantesFromRequestParam();
        return (null);
    }

    public String editSetup() {
        setRestaurantesFromRequestParam();
        return "restaurantes_edit";
    }

    public String edit() {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            restaurantes = em.merge(restaurantes);
            em.getTransaction().commit();
            addSuccessMessage("Restaurantes was successfully updated.");
        } catch (Exception ex) {
            try {
                addErrorMessage(ex.getLocalizedMessage());
                em.getTransaction().rollback();
            } catch (Exception e) {
                addErrorMessage(e.getLocalizedMessage());
            }
        } finally {
            em.close();
        }
        return "restaurantes_list";
    }

    public String destroy() {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            Restaurantes restaurantes = getRestaurantesFromRequestParam();
            restaurantes = em.merge(restaurantes);
            em.remove(restaurantes);
            em.getTransaction().commit();
            addSuccessMessage("Restaurantes was successfully deleted.");
        } catch (Exception ex) {
            try {
                addErrorMessage(ex.getLocalizedMessage());
                em.getTransaction().rollback();
            } catch (Exception e) {
                addErrorMessage(e.getLocalizedMessage());
            }
        } finally {
            em.close();
        }
        return "restaurantes_list";
    }

    public Restaurantes getRestaurantesFromRequestParam() {
        EntityManager em = getEntityManager();
        try{
            Restaurantes o = (Restaurantes) model.getRowData();
            o = em.merge(o);
            return o;
        } finally {
            em.close();
        }
    }

    public void setRestaurantesFromRequestParam() {
        Restaurantes restaurantes = getRestaurantesFromRequestParam();
        setRestaurantes(restaurantes);
    }

    public DataModel getRestaurantess() {
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select object(o) from Restaurantes as o");
            q.setMaxResults(batchSize);
            q.setFirstResult(firstItem);
            model = new ListDataModel(q.getResultList());
            return model;
        } finally {
            em.close();
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, facesMsg);
    }

    public static void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage("successInfo", facesMsg);
    }

    public Restaurantes findRestaurantes(Integer id) {
        EntityManager em = getEntityManager();
        try{
            Restaurantes o = (Restaurantes) em.find(Restaurantes.class, id);
            return o;
        } finally {
            em.close();
        }
    }

    public int getItemCount() {
        EntityManager em = getEntityManager();
        try{
            int count = ((Long) em.createQuery("select count(o) from Restaurantes as o").getSingleResult()).intValue();
            return count;
        } finally {
            em.close();
        }
    }

    public int getFirstItem() {
        return firstItem;
    }

    public int getLastItem() {
        int size = getItemCount();
        return firstItem + batchSize > size ? size : firstItem + batchSize;
    }

    public int getBatchSize() {
        return batchSize;
    }

    public String next() {
        if (firstItem + batchSize < getItemCount()) {
            firstItem += batchSize;
        }
        return "restaurantes_list";
    }

    public String prev() {
        firstItem -= batchSize;
        if (firstItem < 0) {
            firstItem = 0;
        }
        return "restaurantes_list";
    }
    
}
